package com.zwh.modules.datamax.controller;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.excel.ExcelWriter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zwh.autoconfigure.eventbus.EventBusCenter;
import com.zwh.common.lang.DateUtils;
import com.zwh.common.spring.SpringContextUtils;
import com.zwh.common.utils.easyexcel.EasyExcelTemplateExportUtils;
import com.zwh.common.utils.file.SpringUploadUtils;
import com.zwh.modules.datamax.entity.DataMax;
import com.zwh.modules.datamax.eventbus.EventChangeEvent;
import com.zwh.modules.datamax.service.DataMaxService;


/**
 * <p>
 * 数据类型最大值表 前端控制器
 * </p>
 *
 * @author zhangwenhe
 * @since 2020-04-25
 */
@Controller
@RequestMapping("${adminPath}/dataMax")
public class DataMaxController {
    @Autowired
    private DataMaxService dataMaxService;
    @Autowired
    private RedisTemplate<String, Object> template;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private EventBusCenter eventBusCenter;

    @RequestMapping("/list")
    @ResponseBody
    public List<DataMax> list(DataMax dataMax){
        return dataMaxService.findSimpleList(dataMax);
    }

    @RequestMapping("/exportData")
    public void list(DataMax dataMax, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<DataMax> dataMaxList = dataMaxService.findSimpleList(dataMax);
        String fileName = "数据最大值" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        String templatePath = DataMaxController.class.getResource("/").getPath() + "dataMax.xlsx";

        // 列表数据导出
        ExcelWriter excelWriter = EasyExcelTemplateExportUtils.
                fillListToWeb(dataMaxList, DataMax.class, templatePath, fileName, response,false);

        Map<String,Object> map = new HashMap<>();
        map.put("curDate", new Date());
        EasyExcelTemplateExportUtils.fillListToWriteSheet(excelWriter, map, null,
                excelWriter.writeContext().writeSheetHolder().getWriteSheet(), null);
        // 最后需要关闭流
        excelWriter.finish();
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody DataMax dataMax){
        return dataMaxService.insert(dataMax);
    }


    @RequestMapping("/publishMsg")
    @ResponseBody
    public String publishMsg(DataMax dataMax){
        ApplicationContext applicationContext= SpringContextUtils.getApplicationContext();
        EventChangeEvent event = new EventChangeEvent(dataMax.getId());
        eventBusCenter.postAsync(event);
        return "发布成功";
    }


    @RequestMapping("/upload")
    @ResponseBody
    public List<DataMax> upload(HttpServletRequest request){
        try {
            List<SpringUploadUtils.UploadDto> uploadDtos = SpringUploadUtils.uploadForDir(request,"public/20200519/");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @RequestMapping("/testRedisTemplate")
    @ResponseBody
    public Object testRedisTemplate(DataMax dataMax) throws JsonProcessingException {
        ValueOperations<String, Object> redisString = template.opsForValue();
        List<DataMax> list = dataMaxService.findSimpleList(dataMax);
        // SET key value: 设置指定 key 的值
        redisString.set("testList", list);
        // GET key: 获取指定 key 的值
        redisString.set("testList2",objectMapper.writeValueAsString(list) );
        Map<String,Object> map = new HashMap<>();
        map.put("testList",redisString.get("testList"));
        map.put("testList2",redisString.get("testList2"));
        return map;
    }

    @RequestMapping("/testRedisManagerCache")
    @ResponseBody
    @Cacheable(value = "haha1")
    public List<DataMax> findSimpleList(DataMax dataMax){
        return dataMaxService.findSimpleList(dataMax);
    }

}
